<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\RepaymentPlanConsume;

class RepaymentPlanConsumeRepository
{
    /**
     * 创建用户绑定的还款计划
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return RepaymentPlanConsume::create($params);
    }

    /**
     * 根据id获取还款计划资料
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return RepaymentPlanConsume::find($id);
    }

    /**
     * 根据还款计划id获取每天还款计划
     * @param $id
     * @return mixed
     */
    public function planRepaymentConsumeAll($planRepaymentId)
    {
        return RepaymentPlanConsume::where('plan_repayment_id', $planRepaymentId)->get();
    }

    public function byFailMessage($repaymentPlanId)
    {
        return RepaymentPlanConsume::where([
            ['repayment_plan_id', '=', $repaymentPlanId],
            ['fail_message', '<>', ''],
        ])->first();
    }

    public function byStatus($repaymentPlanId, $status)
    {
        return RepaymentPlanConsume::where([
            'repayment_plan_id' => $repaymentPlanId,
            'repayment_consume_status' => $status,
        ])->first();
    }

    public function byStatusConsumeServiceChargeSum($repaymentPlanId, $status)
    {
        return RepaymentPlanConsume::where([
            'repayment_plan_id' => $repaymentPlanId,
            'repayment_consume_status' => $status,
        ])->sum('consume_service_charge');
    }

    public function planRepaymentConsumeMoneySum($planRepaymentId)
    {
        return RepaymentPlanConsume::where('plan_repayment_id', $planRepaymentId)
            ->whereIn('repayment_consume_status', [2000, 2003])
            ->sum('consume_money');
    }

    /**
     * 根据还款计划id获取所有消费
     * @param $id
     * @return mixed
     */
    public function repaymentPlanConsumeAllToDate($repaymentPlanId, $consumeDate)
    {
        return RepaymentPlanConsume::where([
            'repayment_plan_id' => $repaymentPlanId,
            'consume_date' => $consumeDate,
        ])->get();
    }

    /**
     * 根据还款计划id获取所有消费
     * @param $id
     * @return mixed
     */
    public function repaymentPlanConsumeAll($repaymentPlanId)
    {
        return RepaymentPlanConsume::where('repayment_plan_id', $repaymentPlanId)->get();
    }

    /**
     * 更新单号
     * @param $id
     * @param $consumeSn
     * @return mixed
     */
    public function updateSnById($id, $consumeSn)
    {
        return RepaymentPlanConsume::where('id', $id)->update([
            'plan_consume_sn' => $consumeSn,
        ]);
    }

    /**
     * 更新状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function updateStatusById($id, $status, $failMessage)
    {
        return RepaymentPlanConsume::where('id', $id)->update([
            'repayment_consume_status' => $status,
            'fail_message' => $failMessage,
        ]);
    }

    /**
     * 更新状态
     * @param $id
     * @param $status
     * @param $failMessage
     * @return mixed
     */
    public function updateStatusByConsumeSn($consumeSn, $status, $failMessage = '')
    {
        return RepaymentPlanConsume::where('plan_consume_sn', $consumeSn)
            ->update([
                'repayment_consume_status' => $status,
                'fail_message' => $failMessage,
            ]);
    }

    public function byConsumeSn($consumeSn)
    {
        return RepaymentPlanConsume::where('plan_consume_sn', $consumeSn)->first();
    }
    
}